﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class TKD09 : ViewBase
    {
        #region ==dwKDF===========
        public static readonly DependencyProperty dwKDFProperty = DependencyProperty.Register("dwKDF",
            typeof(DataRow), typeof(TKD09),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwKDFChanged));
        public DataRow dwKDF
        {
            get
            {
                return (DataRow)GetValue(dwKDFProperty);
            }
            set
            {
                SetValue(dwKDFProperty, value);
            }
        }
        private static void OdwKDFChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TKD09;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwKDF = row; 
            }
        }
        #endregion 
        #region ==dwKD===========
        public static readonly DependencyProperty dwKDProperty = DependencyProperty.Register("dwKD",
            typeof(DataRow), typeof(TKD09),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwKDChanged));
        public DataRow dwKD
        {
            get
            {
                return (DataRow)GetValue(dwKDProperty);
            }
            set
            {
                SetValue(dwKDProperty, value);
            }
        }
        private static void OdwKDChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TKD09;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwKD = row; 
            }
        }
        #endregion  
        #region ==KDF===========
        public static readonly DependencyProperty KDFProperty = DependencyProperty.Register("KDF",
            typeof(string), typeof(TKD09),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnKDFChanged));
        public string KDF
        {
            get
            {
                return (string)GetValue(KDFProperty);
            }
            set
            {
                SetValue(KDFProperty, value);
            }
        }
        private static void OnKDFChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TKD09;
            if (src != null)
            {
                src.KDF = (string)e.NewValue; 
            }
        }
        #endregion
        public TKD09()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VKUNDFORM(); 
            IsMasterFromMV = true;
            TBMain = MV.SetProd.Tables[TB];
            InitializeComponent();
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwKDF = dwMaster;
            if (dwKDF != null)
            {
               
            }
        }
        DataSet SResult;
        DataRow dwData;
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e); 
            Load();
        } 
        public void Load()
        { 
            dwData = TBMain.NewRow(); 
            dwData["KDFMANDANT"] = app.Mandant;
            dwData["KDFFORMART"] = "E";
            SResult = ((VKUNDFORM)MV).QueryKUNDFORM(dwData, 2);
            MergeData(SResult, true,true);
        }
        public void Locate()
        {
            if (TBMain == null)
                return;
            if (TBMain.Rows.Count == 0)
                return;
            int idx = 0;
            var query = from DataRow row in TBMain.AsEnumerable()
                        where row["KDFKUNDNR"].ToString().Trim() == dwKD["KDKUNDNR"].ToString().Trim()
                        select row;
            foreach (DataRow row in query)
            {
                idx = TBMain.Rows.IndexOf(row);
                break;
            }
            vwMaster.View.MoveCurrentTo(idx);
        }

        public override void Copy()
        {
            base.Copy();
            if (dwKDF == null)
                return;
            dwData = TBMain.NewRow();
            dwData.ItemArray = dwKDF.ItemArray;
            dwData["KDFKUNDNR"] = dwKDF["KDFKUNDNR"];
            dwData["KDKUNDNA"] = dwKDF["KDFKUNDNA"];
            dwData["KDFNEUD"] = MV.date;
            dwData["KDFNEUB"] = app.User;
            dwData["KDFFORMULAR1"] = "W0";
            Insert(dwData);
        }
        public override void New()
        {
            base.New();
            try
            {
                if (dwKD == null)
                {
                    MessageBox.Show("请务必输入目标客户名称");
                    return;
                }
                dwData =TBMain.NewRow();
                ((VKUNDFORM)MV).SetUpNewRow(ref dwData);
                if (KDF.Length < 1)
                {
                    KDF = "W" + Helpers.Helper.GetKDKUNDNR(dwKDF["KDFKUNDNR"].ToString());
                } 
                dwData["KDFKUNDNR"] = dwKDF["KDFKUNDNR"];
                dwData["KDKUNDNA"] = dwKDF["KDFKUNDNA"];
                dwData["KDFNEUD"] = MV.date;
                dwData["KDFNEUB"] = app.User;
                dwData["KDFFORMULAR1"] = KDF;
                Insert(dwData);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void Insert(DataRow _dwKDF)
        {
            TBMain.Rows.InsertAt(_dwKDF, 0);
            vwMaster.View.MoveCurrentToFirst();
            ((VKUNDFORM)MV).InsertKUNDFORM(_dwKDF, app.company);
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            New();
            base.NewCommand(sender, e);
        }
        public override void Update()
        {
            base.Update();
            ((VKUNDFORM)MV).UpdateKUNDFORM(dwMaster, 0);
        }
        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            KDF=dwData["KDFFORMULAR1"].ToString();
            base.EditCommand(sender, e);
        }
        public override void CopyCommand(object sender, RoutedEventArgs e)
        {
            Copy();
            base.CopyCommand(sender, e);
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            Update();
            base.SaveCommand(sender, e);
        } 
    }
}
